import wx, sys
from com.nasgektw.cards.client.model import DeckGenerator
from com.nasgektw.cards.client.model.Configuration import Configuration
from com.nasgektw.cards.client.gui.ViewerFrame import ViewerFrame
from com.nasgektw.cards.client.gui.ListCtrl import CheckListCtrl, EditableListCtrl
class ConfigureTeasePanel(wx.Panel):
    def __init__(self, *args, **kwds):
        wx.Panel.__init__(self, *args, **kwds)
        self.slidesNumberField = wx.SpinCtrl(self)
        self.slidesNumberField.SetRange(0, sys.maxint)
        self.slidesNumberField.SetValue(Configuration.defaultTime)
        self.strokesField = wx.SpinCtrl(self)
        self.strokesField.SetRange(0, sys.maxint)
        self.strokesField.SetValue(Configuration.defaultStroke)
        self.displayTimeField = wx.SpinCtrl(self)
        self.displayTimeField.SetRange(1, sys.maxint)
        self.displayTimeField.SetValue(Configuration.defaultDisplayTime)
        self.picturesPerCardField = wx.SpinCtrl(self)
        self.picturesPerCardField.SetRange(0, sys.maxint)
        self.picturesPerCardField.SetValue(Configuration.defaultPicturesPerCard)
        self.picturesFolderField = wx.TextCtrl(self)
        self.picturesFolderSelectionButton = wx.Button(self, wx.ID_ANY, "Choose...")
        self.deckField = wx.ComboBox(self)
        self.slidesNumberLabel = wx.StaticText(self, wx.ID_ANY, 'Number of slides:')
        self.strokesLabel = wx.StaticText(self, wx.ID_ANY, 'Strokes per slides:')
        self.displayLabel = wx.StaticText(self, wx.ID_ANY, 'Display time (s):')
        self.picturesPerCardLabel = wx.StaticText(self, wx.ID_ANY, 'Number of pictures per card:')
        self.picturesFolderLabel = wx.StaticText(self, wx.ID_ANY, 'Pictures folder:')
        self.deckLabel = wx.StaticText(self, wx.ID_ANY, 'Deck:')
        self.randomDeckPanel = wx.Panel(self)
        self.categoriesField = CheckListCtrl(self.randomDeckPanel)
        self.categoriesField.InsertColumn(0, 'Category')
        self.typesField = EditableListCtrl(self.randomDeckPanel)
        self.typesField.InsertColumn(0, 'Type')
        self.typesField.InsertColumn(1, 'Ratio %')
        self.startTeasingButton = wx.Button(self, wx.ID_ANY, "Start")
        self.startTeasingButton.Enable(False)
        self.viewerFrame = ViewerFrame(self)
        self.__do_binding__()
        self.__do_layout__()
    def refreshCards(self):
        self.deckField.Clear()
        self.deckField.AppendItems(['< Random >'] + Configuration.getDecks())
        for category in Configuration.getCategories():
            index = self.categoriesField.InsertStringItem(sys.maxint, category)
            self.categoriesField.CheckItem(index, True)
        for cardType in Configuration.getTypes():
            index = self.typesField.InsertStringItem(sys.maxint, cardType)
            self.typesField.SetStringItem(index, 1, str(0))
        self.startTeasingButton.Enable(bool(Configuration.cards) and bool(self.deckField.GetValue()) and bool(self.picturesFolderField.GetValue()))
    def __on_select_pictures_folder__(self, event):
        dialog = wx.DirDialog(None, "Choose a directory:", style = wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dialog.ShowModal() == wx.ID_OK:
            if self.picturesFolderField.GetValue() != dialog.GetPath():
                self.picturesFolderField.SetValue(dialog.GetPath())
                self.startTeasingButton.Enable(bool(Configuration.cards) and bool(self.deckField.GetValue()) and bool(self.picturesFolderField.GetValue()))
        dialog.Destroy()
    def __do_binding__(self):
        self.picturesFolderSelectionButton.Bind(wx.EVT_BUTTON, self.__on_select_pictures_folder__)
        self.deckField.Bind(wx.EVT_COMBOBOX, self.__change_deck__)
        self.typesField.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.__on_begin_type_edit__)
        self.startTeasingButton.Bind(wx.EVT_BUTTON, self.__start_teasing__)
    def __start_teasing__(self, event):
        if self.deckField.GetValue() == '< Random >':
            categories = []
            for index in range(self.categoriesField.GetItemCount()):
                if self.categoriesField.IsChecked(index):
                    categories.append(self.categoriesField.GetItem(index).GetText())
            types = []
            for index in range(self.typesField.GetItemCount()):
                types.append((self.typesField.GetItem(index).GetText(), int(self.typesField.GetItem(index, 1).GetText())))
            generator = DeckGenerator.DeckGeneratorRandomly(categories, types, self.picturesFolderField.GetValue(), int(self.slidesNumberField.GetValue()), int(self.slidesNumberField.GetValue())/(int(self.picturesPerCardField.GetValue()) + 1))
        else:
            generator = DeckGenerator.DeckGeneratorByDeck(self.deckField.GetValue(), self.picturesFolderField.GetValue(), int(self.slidesNumberField.GetValue()), int(self.slidesNumberField.GetValue())/(int(self.picturesPerCardField.GetValue()) + 1))
        self.viewerFrame.display(int(self.strokesField.GetValue()), int(self.displayTimeField.GetValue()), generator.generate())
    def __on_begin_type_edit__(self, event):
        if event.m_col == 0:
            event.Veto()
        else:
            event.Skip()
    def __change_deck__(self, event):
        if self.deckField.GetValue() == '< Random >':
            self.randomDeckPanel.Show()
            self.Layout()
        else:
            self.randomDeckPanel.Hide()
        self.startTeasingButton.Enable(bool(Configuration.cards) and bool(self.deckField.GetValue()) and bool(self.picturesFolderField.GetValue()))
    def __do_layout__(self):
        randomPanelSizer = wx.GridBagSizer(5,5)
        randomPanelSizer.Add(self.categoriesField, (0,0), wx.DefaultSpan, wx.EXPAND | wx.ALL, 0)
        randomPanelSizer.Add(self.typesField, (1,0), wx.DefaultSpan, wx.EXPAND | wx.ALL, 0)
        randomPanelSizer.AddGrowableCol(0, 1)
        randomPanelSizer.AddGrowableRow(0, 1)
        randomPanelSizer.AddGrowableRow(1, 1)
        self.randomDeckPanel.SetSizer(randomPanelSizer)
        self.randomDeckPanel.Hide()
        sizer = wx.GridBagSizer(5,5)
        sizer.Add(self.picturesFolderLabel, (0,0), wx.DefaultSpan, wx.ALL, 0)
        sizer.Add(self.picturesFolderField, (0,1), wx.DefaultSpan, wx.EXPAND|wx.ALL, 0)
        sizer.Add(self.picturesFolderSelectionButton, (0,2), wx.DefaultSpan, wx.ALL, 0)
        sizer.Add(self.slidesNumberLabel, (1,0), wx.DefaultSpan, wx.ALL, 0)
        sizer.Add(self.slidesNumberField, (1,1), (1,2), wx.EXPAND|wx.ALL, 0)
        sizer.Add(self.strokesLabel, (2,0), wx.DefaultSpan, wx.ALL, 0)
        sizer.Add(self.strokesField, (2,1), (1,2), wx.EXPAND|wx.ALL, 0)
        sizer.Add(self.displayLabel, (3,0), wx.DefaultSpan, wx.ALL, 0)
        sizer.Add(self.displayTimeField, (3,1), (1,2), wx.EXPAND|wx.ALL, 0)
        sizer.Add(self.picturesPerCardLabel, (4,0), wx.DefaultSpan, wx.ALL, 0)
        sizer.Add(self.picturesPerCardField, (4,1), (1,2), wx.EXPAND|wx.ALL, 0)
        sizer.Add(self.deckLabel, (5,0), wx.DefaultSpan, wx.ALL, 0)
        sizer.Add(self.deckField, (5,1), (1,2), wx.EXPAND|wx.ALL, 0)
        sizer.Add(self.randomDeckPanel, (6,0), (1,3), wx.EXPAND|wx.ALL, 0)
        sizer.Add(self.startTeasingButton, (7,2), wx.DefaultSpan, wx.ALL | wx.ALIGN_RIGHT, 0)
        sizer.AddGrowableCol(1, 1)
        sizer.AddGrowableRow(6, 1)
        self.SetSizer(sizer)